import com.beans.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * Created with IntelliJ IDEA.
 * Description:第三步添加一个启动类
 * User: lenovo
 * Date: 2023-01-08
 * Time: 13:23
 */
public class APP {
    public static void main1(String[] args) {
        System.out.println("你好spring!");
    }

    public static void main2(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        //使用beanname获取bean对象 要强制类型转换 如果是null强转会报错
        User user = (User) context.getBean("user");
        user.sayHi("beanname");

        //getBean的三种重载
        //使用类对象获取,不用进行类型转换 不适用于有多个相同的bean对象
        User user1 = context.getBean(User.class);
        user1.sayHi("beantype");

        //使用beanname和类对象获取  使用比较多,不应进行类型转换
        User user2 = context.getBean("user",User.class);
        user.sayHi("beanname+beantype");

    }

    //BeanFactory是ApplicationContext的父类,BeanFactory是懒汉模式,ApplicationContext是饿汉模式(在创建会将全部的bean对象都加载出来)
    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
        User user = factory.getBean("user",User.class);
        user.sayHi("BeanFactory");
    }
}
